package com.jerry.secondkill.rabbitmq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
	
	public static final String QUEUE_SECKILL = "queue.seckill";
	public static final String QUEUE = "queue";
	public static final String QUEUE_1 = "queue.1";
	public static final String QUEUE_2 = "queue.2";
	public static final String QUEUE_3 = "queue.3";
	public static final String EXCHANGE_TOPIC = "exchage.topic";
	public static final String EXCHANGE_FANOUT = "exchage.fanout";
	public static final String EXCHANGE_HEADERS = "exchage.headers";
	
	
	//创建秒杀队列
	@Bean
	public Queue killQueue() {
		return new Queue(QUEUE_SECKILL, true);
	}
	
	//创建队列1
	@Bean
	public Queue topicQueue1() {
		return new Queue(QUEUE_1, true);
	}
	//创建队列2
	@Bean
	public Queue topicQueue2() {
		return new Queue(QUEUE_2, true);
	}
	//创建队列3
	@Bean
	public Queue headerQueue() {
		return new Queue(QUEUE_3, true);
	}
		
		
	
	/**
	 * Direct模式
	 * */
	@Bean
	public Queue directQueue() {
		return new Queue(QUEUE, true);
	}
	
	
	/**
	 * Topic模式：匹配模式，只有匹配上才触发
	 * */
	@Bean
	public TopicExchange topicExchage(){
		//创建一个topic交换机
		return new TopicExchange(EXCHANGE_TOPIC);
	}
	@Bean
	public Binding topicBinding1() {
		return BindingBuilder.bind(topicQueue1()).to(topicExchage()).with("topic.key1");
	}
	@Bean
	public Binding topicBinding2() {
		return BindingBuilder.bind(topicQueue2()).to(topicExchage()).with("topic.#");
	}
	
	
	/**
	 * Fanout模式:广播模式
	 * */
	@Bean
	public FanoutExchange fanoutExchage(){
		return new FanoutExchange(EXCHANGE_FANOUT);
	}
	@Bean
	public Binding FanoutBinding1() {
		return BindingBuilder.bind(topicQueue1()).to(fanoutExchage());
	}
	@Bean
	public Binding FanoutBinding2() {
		return BindingBuilder.bind(topicQueue2()).to(fanoutExchage());
	}
	
	
	/**
	 * Header模式：只有同时满足map中的所有条件，才能入队列
	 * */
	@Bean
	public HeadersExchange headersExchage(){
		return new HeadersExchange(EXCHANGE_HEADERS);
	}
	@Bean
	public Binding headerBinding() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("header1", "value1");
		map.put("header2", "value2");
		return BindingBuilder.bind(headerQueue()).to(headersExchage()).whereAll(map).match();
	}
	
	
}
